package com.nondo.rdp.auth.endpoint;

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.nondo.rdp.core.common.AliyunVideoTool;
import com.nondo.rdp.core.model.Video;
import com.nondo.rdp.core.model.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * com.nondo.rdp.auth.endpoint
 *
 * @author zhaolijin
 * @since 2017/10/24
 */
@RestController
@RequestMapping(value = "/api/sys/vod")
public class AuthEndpoint {

    @Autowired
    AliyunVideoTool aliyunVideoTool;

    /**
     * 获取上传地址和凭证
     *
     * @param request
     * @param response
     * @param video
     * @return
     */
    @RequestMapping(value = "/upload/auth")
    public View<CreateUploadVideoResponse> auth(HttpServletRequest request,
                                                HttpServletResponse response, @RequestBody Video video) {
        View<CreateUploadVideoResponse> view = new View<CreateUploadVideoResponse>(
                20000, "授权成功");
        try {

            CreateUploadVideoResponse videoResponse = aliyunVideoTool
                    .createUploadVideo(video);
            view.setData(videoResponse);
        } catch (ClientException e) {
            e.printStackTrace();
            return View.ofError("获取上传地址和凭证失败");
        }
        return view;
    }

    /**
     * 刷新视频上传凭证
     *
     * @param request
     * @param response
     * @param video
     * @return
     */
    @RequestMapping(value = "/refresh/auth")
    public View<RefreshUploadVideoResponse> refreshAuth(
            HttpServletRequest request, HttpServletResponse response,
            @RequestBody Video video) {
        View<RefreshUploadVideoResponse> view = new View<RefreshUploadVideoResponse>(
                20000, "授权成功");
        try {
            RefreshUploadVideoResponse videoResponse = aliyunVideoTool
                    .refreshUploadVideo(video.getVideoId());
            view.setData(videoResponse);
        } catch (ClientException e) {
            e.printStackTrace();
            return View.ofError("刷新视频上传凭证失败");
        }
        return view;
    }

}
